package com.lanou.bookstore.book.dao;

import com.lanou.bookstore.book.domain.Book;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {

    // c3p0初始化
    private ComboPooledDataSource dataSource = new ComboPooledDataSource();
    @Override
    public List<Book> queryAll() {

        try {
            Connection conn = dataSource.getConnection();

            QueryRunner qr = new QueryRunner();
            List<Book> clist = qr.query(conn,
                    "select * from book",
                    new BeanListHandler<Book>(Book.class));

            DbUtils.close(conn);
            return clist;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Book> queryByCate(String cate) {

        try {
            Connection conn = dataSource.getConnection();

            QueryRunner qr = new QueryRunner();
            List<Book> clist = qr.query(conn,
                    "select * from bookstore.book\n" +
                            "join category c on book.cid = c.cid\n" +
                            "where c.cname = ? ",
                    new BeanListHandler<Book>(Book.class),
                    cate);

            DbUtils.close(conn);
            return clist;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Book> queryByDid(String bId) {

        try {
            Connection conn = dataSource.getConnection();

            QueryRunner qr = new QueryRunner();
            List<Book> clist = qr.query(conn,
                    "select * from book\n" +
                            "where bid = ? ",
                    new BeanListHandler<Book>(Book.class),
                    bId);

            DbUtils.close(conn);
            return clist;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void addBook(Book book, int bid) {
        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            int result = qr.update(conn, "insert into book (bid,bname,price,author,image,cid) values(?,?,?,?,?,?)"
                    , bid,book.getBname(), book.getPrice(),book.getAuthor(),book.getImage(),book.getCid());
            DbUtils.close(conn);



        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delBook(String bookid) {


        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            qr.update(conn, "delete from book where bid = ?"
                    , bookid);
            DbUtils.close(conn);



        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void modBook(Book book,String bookid) {

        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            qr.update(conn, "update book set bname = ?, price = ?, author = ?, cid = ? " +
                            "where bid = ?"
                    , book.getBname(),book.getPrice(),book.getAuthor(),book.getCid(),bookid);
            DbUtils.close(conn);



        } catch (SQLException e) {
            e.printStackTrace();
        }



    }
}
